package com.mixoft.TestApp;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;
import com.mixoft.TestApp.R;
import com.viewpagerindicator.TitlePageIndicator;

public abstract class BasePagerFragment extends SherlockFragment {

	// Internal components
	ViewPager pager;
	
	/*
	 * Defines the function that returns layouts for the pager and
	 * their corresponding titles
	 */
	abstract int[] pages();
	abstract int[] titles();
	
	/** Called when the activity is first created. */
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.activity_pager_base, container, false);
	    
	    // Set up the UI
		setupPagerAdapter(this.pages(), this.titles(), view);
	    setupViewPagerIndicator(pager, view);
		
	    return view;
	}
	
	/*
	 * Sets up our custom pager adapter
	 * The function receives resource ID's of the layouts to be viewed
	 * and their respective titles
	 */
	private void setupPagerAdapter(int[] resLayouts, int[] resTitles, View currView)
	{
		// Load title strings from resources
		String[] titles = new String[resTitles.length];
		for (int i = 0; i < titles.length; i++)
			titles[i] = getSherlockActivity().getString(resTitles[i]);

		// Init the pager adapter
		BaseFragmentPagerAdapter adapter = 
			new BaseFragmentPagerAdapter(resLayouts, titles,
					getSherlockActivity().getSupportFragmentManager());
	    pager = (ViewPager) currView.findViewById(R.id.viewpagerBase);
	    pager.setAdapter(adapter);
	}
	
	/*
	 * Defines the logic of setting the current page in the child object
	 */
	abstract void setCurrentPage();
	
	/*
	 * Sets up ViewPagerIndicator
	 */
	private void setupViewPagerIndicator(ViewPager pager, View currView)
	{
		//Bind the title indicator to the adapter
		TitlePageIndicator titleIndicator = 
			(TitlePageIndicator)currView.findViewById(R.id.titles);
		titleIndicator.setViewPager(pager);
	}
	
}
